package com.hackordie.daos;

import com.hackordie.commons.authorities.User;

public class UserDao extends ACommonDao<User> {

	@Override
	public boolean create(final User entity) {
		final String SQL = "INSERT INTO users (idUser, userName, userPassword, email, signinDate, enabled, activationCode) VALUES (?, ?, ?, ?, ?, ?, ?)";
		jdbcTemplate.update(SQL, entity.toArray());
		return true;
	}

	@Override
	public boolean delete(final int idUser) {
		final String SQL = "DELETE FROM users WHERE idUser = ?";
		jdbcTemplate.update(SQL, idUser);
		return false;
	}

	public boolean delete(String userName) {
		final String SQL = "DELETE FROM users WHERE userName = ?";
		jdbcTemplate.update(SQL, userName);
		return false;
	}

	@Override
	public boolean update(final User entity) {
		final String SQL = "UPDATE users SET userName = ?, userPassword = ?, enabled = ? WHERE idUser = ?";
		jdbcTemplate.update(SQL, entity.toArray(), entity.getId());
		return true;
	}

	@Override
	public User[] getAll() {
		final String SQL = "SELECT * FROM users";
		return jdbcTemplate.query(SQL, new UserMapper()).toArray(new User[] {});
	}

	public User[] getByRange(int first, int last) {
		final String SQL = "SELECT * FROM hod.Users LIMIT ?,?";
		return jdbcTemplate.query(SQL, new UserMapper(), first, last).toArray(new User[] {});
	}

	public int countAll() {
		final String SQL = "Select COUNT(idUser) FROM users";
		return jdbcTemplate.queryForObject(SQL, Integer.class);
	}

	@Override
	public User get(int idUser) {
		final String SQL = "SELECT * FROM users WHERE idUser = ?";
		return jdbcTemplate.queryForObject(SQL, new UserMapper(), idUser);
	}

	public User get(String userName) {
		final String SQL = "SELECT * FROM users WHERE userName = ?";
		return jdbcTemplate.queryForObject(SQL, new UserMapper(), userName);
	}

}
